﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pawn
{
    public partial class FrmKhach : Form
    {
        DataClassesDataContext db;
        public bool Mode { set; get; }
        public String MaKH { get { return dgKhach.SelectedRows[0].Cells["Id"].Value.ToString(); } }
        int currentRow = 0;
        public FrmKhach()
        {
            InitializeComponent();
            db = new DataClassesDataContext();
            Mode = false;
        }

        private void FrmKhach_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            DataSet.KhachDataTable dt = new DataSet.KhachDataTable();
            var q = from k in db.Khaches
                    where k.HoTen.Contains(tbTuKhoa.Text) || k.DiaChi.Contains(tbTuKhoa.Text) || k.LienLac.Contains(tbTuKhoa.Text) || k.CMND.Contains(tbTuKhoa.Text)
                    || k.Id.ToString().Contains(tbTuKhoa.Text)
                    select k;
            foreach (var k in q)
            {
                var q1 = from p in k.PhieuCams
                         where p.DaTraTien==false
                         select p;
                dt.Rows.Add(k.Id, k.HoTen, k.LienLac, k.DiaChi, k.CMND, k.GioiTinh==null?"":(bool)k.GioiTinh?"Nam":"Nữ",k.PhieuCams.Count,q1.Count());
            }
            dgKhach.DataSource = dt;
            if(currentRow<dgKhach.Rows.Count) dgKhach.Rows[currentRow].Selected = true;
        }

        private void tbTuKhoa_TextChanged(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = dgKhach.SelectedRows;
            if (rows.Count < 1) return;
            //MessageBox.Show(rows[0].Cells["Id"].Value.ToString());
            var q = from k in db.Khaches
                    where k.Id == Convert.ToInt32(rows[0].Cells["Id"].Value)
                    select k;
            foreach (Khach k in q)
            {
                var q1 = from p in k.PhieuCams
                         where p.DaTraTien == false
                         select p;
                if (q1.Count() > 0)
                {
                    MessageBox.Show("Khách hàng này vẫn còn phiếu chưa trả đồ!");
                    return;
                }
                if (MessageBox.Show("Bạn có chắc muốn xóa khách hàng này? Những phiếu vay của khách hàng này cũng sẽ bị xóa!", "Cảnh Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    db.Khaches.DeleteOnSubmit(k);
                    db.SubmitChanges();
                    timer1.Start();
                }
                break;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FrmThongTinKhach frm = new FrmThongTinKhach();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Khach k = new Khach() { HoTen = frm.HoTen, DiaChi = frm.DiaChi, LienLac = frm.DienThoai, CMND = frm.CMND };
                if (frm.GioiTinh == 1) k.GioiTinh = true;
                else if (frm.GioiTinh == 2) k.GioiTinh = false;
                db.Khaches.InsertOnSubmit(k);
                db.SubmitChanges();
                timer1.Start();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgKhach.SelectedRows.Count < 1) return;
            DataGridViewRow r = dgKhach.SelectedRows[0];
            currentRow = r.Index;
            var q = from k in db.Khaches
                    where k.Id == Convert.ToInt32(r.Cells["Id"].Value)
                    select k;
            foreach (var k in q)
            {
                FrmThongTinKhach frm = new FrmThongTinKhach();
                frm.HoTen = k.HoTen;
                frm.DiaChi = k.DiaChi;
                frm.DienThoai = k.LienLac;
                frm.CMND = k.CMND;
                frm.GioiTinh = k.GioiTinh == null ? 0 : k.GioiTinh == true ? 1 : 2;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    k.HoTen = frm.HoTen;
                    k.DiaChi = frm.DiaChi;
                    k.LienLac = frm.DienThoai;
                    k.CMND = frm.CMND;
                    if (frm.GioiTinh == 1) k.GioiTinh = true;
                    else if (frm.GioiTinh == 2) k.GioiTinh = false;
                    db.SubmitChanges();
                    timer1.Start();
                }
                break;
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgKhach_DoubleClick(object sender, EventArgs e)
        {
            if (Mode)
            {
                DialogResult = DialogResult.OK;
                this.Close();
            }
            else
                btnSua_Click(sender, e);
        }

        private void dgKhach_SelectionChanged(object sender, EventArgs e)
        {
        }
    }
}
